package com.gexiaobao.pigeon.ui.race

import android.os.Bundle
import android.text.TextUtils
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentRaceBinding
import com.gexiaobao.pigeon.ui.activity.ActivityGameResult
import com.gexiaobao.pigeon.ui.adapter.MatchListAdapter
import com.gexiaobao.pigeon.viewmodel.request.RequestMatchViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.util.startActivity

/**
 * 比赛
 */
class RaceFragment : BaseFragment<RaceViewModel, FragmentRaceBinding>() {

    private val mAdapter: MatchListAdapter by lazy { MatchListAdapter(arrayListOf()) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    //请求数据ViewModel
    private val requestHomeViewModel: RequestMatchViewModel by viewModels()

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            requestHomeViewModel.getRace()
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            requestHomeViewModel.getRace()
        }

        mAdapter.addChildClickViewIds(R.id.go_race_pass)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            if (mAdapter.data[position].racingPassId == 0 || TextUtils.isEmpty(mAdapter.data[position].name)) {
                RxToast.showToast(getString(R.string.There_are_no_ongoing_levels))
                return@setOnItemChildClickListener
            }
            startActivity<ActivityGameResult>(
                "title" to mAdapter.data[position].name,
                "passId" to mAdapter.data[position].racingPassId,
                "type" to "race"
            )
            activity?.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                val mRaceId = mAdapter.data[position].id
                val mOrgId = mAdapter.data[position].orgId
                val mRaceName = mAdapter.data[position].name
                val orgType = mAdapter.data[position].orgType
                if (orgType == 2) {
                    nav().navigateAction(R.id.action_to_org_info, Bundle().apply {
                        putInt("orgId", mOrgId)
                        putString("raceName", mRaceName)
                        putInt("raceId", mRaceId)
                        putString("type", "race")
                    })
                } else {
                    nav().navigateAction(R.id.action_to_match, Bundle().apply {
                        putString("title", mRaceName)
                        putInt("id", mRaceId)
                        putInt("racingPassId", mAdapter.data[position].racingPassId)
                        putInt("followStatus", 1)
                    })
                }
            }
        }
    }

    override fun lazyLoadData() {
        requestHomeViewModel.getRace()
    }

    override fun createObserver() {
        requestHomeViewModel.run {
            raceList.observe(viewLifecycleOwner) {
                loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            }
        }
    }

}